package unit2;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;

import unit2.creater.CreaterTest;
import unit2.question.Question;

/**
 * Test, where test initialization and start, show  count of correct answers
 * @author Nick
 */

public class Test  implements  Serializable{  
	private static final long serialVersionUID = 8686671464340940408L;
	private ArrayList<Question> arrayOfQuestion;  
	private ArrayList<Integer> scoreOfEachQuestion;
	private int currentPosition;
	private final int numberOfChooseQuestion=80;
	private final int numberOfEnterQuestion=0;
	private final int numberOfYesNoQuestion=0;

	public Test(){
		CreaterTest first= new CreaterTest();
		arrayOfQuestion= new ArrayList<Question>();
		scoreOfEachQuestion= new ArrayList<Integer> (numberOfChooseQuestion+numberOfEnterQuestion+numberOfYesNoQuestion);
		initAnswer();
		arrayOfQuestion=first.createTest(numberOfChooseQuestion,numberOfEnterQuestion,numberOfYesNoQuestion);
		currentPosition=0;		
	}
	
	
	public void nextQuestion(){
		if(currentPosition!=getLength()){
		showQuestion(arrayOfQuestion.get(currentPosition+1));
		addAnswer(currentPosition+1);	
		currentPosition++;
		}
	}
	
	public void previousQuestion(){
		if(currentPosition!=0){
		showQuestion(arrayOfQuestion.get(currentPosition-1));
		addAnswer(currentPosition-1);	
		currentPosition--;
		}
	}
	
	private void showQuestion(Question questionForShow){
		System.out.println(questionForShow.getQuestion());
	}
		
	private void addAnswer(int index){	
		if((setAnswer()).equals(arrayOfQuestion.get(index).getAnswer())){	
		scoreOfEachQuestion.set(index, arrayOfQuestion.get(index).getScore());
		}
		else
			scoreOfEachQuestion.set(index, 0);
	}
	
	private String setAnswer(){
		  Scanner inputData = new Scanner (System.in);
		  return inputData.nextLine();
	}
	
	public  void showScore(){
		System.out.println("Your Score : "+ sumScore());
	}
	
	public int sumScore(){
		Integer sum=0;
		for( int e: scoreOfEachQuestion)
			sum+=e;
		
		return sum;
	}
	
	
	private int getLength(){
		return arrayOfQuestion.size();
	}
	
	
	private void initAnswer(){
		for(int i=0; i<this.numberOfChooseQuestion+this.numberOfEnterQuestion+this.numberOfYesNoQuestion; i++){
			scoreOfEachQuestion.add(0);
		}
	}
	
}